﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DbFactory
{
    public interface IDatabaseConnection
    {
        void connect();

        void connect(string connectionString);

        void disconnect();


        ConnectionState connectState();

        void Execute(string sql, object param = null, IDbTransaction transaction = null);

        void insert<T>(T entity, DbTransaction transaction = null);

        Task insertAsync<T>(T entity, DbTransaction transaction = null);

        void update<T>(T entity, DbTransaction transaction = null, string key = "Id");

        void delete<T>(string id, string keyName = "id", DbTransaction transaction = null);

        void delete<T>(string id);
        void delete<T>(Expression<Func<T, bool>> expression, DbTransaction transaction = null);

        DbTransaction BeginTransaction();

        IEnumerable<T> QueryList<T>();

        IEnumerable<T> QueryList<T>(int pageSize, int page, string orderby = "");

        IEnumerable<T> QueryList<T>(string where);

        IEnumerable<T> QueryList<T>(string where, int pageSize, int page);

        T QueryFirst<T>(string id, string keyName = "id");

        T QueryFirstWhere<T>(Expression<Func<T, bool>> expression);
        T QueryFirstWhere<T>(string where);

        IEnumerable<T> QuerySql<T>(string sql, object param = null);

        T QuerySqlOne<T>(string sql, object param = null);

        IEnumerable<T> QuerySql<T>(string sql, int pageSize, int page, object param = null);

        void CreateTable<T>();

        int GetCount<T>(string where = "");


        IEnumerable<T> QueryList<T>(Expression<Func<T, bool>> expression);

        IEnumerable<T> QueryList<T>(Expression<Func<T, bool>> expression, int pageSize, int page);


        IEnumerable<TResult> QueryJoin<T, TJoin, TResult>(
            Expression<Func<T, TJoin, bool>> joinExpression,
            Expression<Func<T, TJoin, TResult>> selectExpression,
            Expression<Func<T, TJoin, bool>> whereExpression = null);

        IEnumerable<TResult> QueryJoin<T, TJoin, TResult>(
            Expression<Func<T, TJoin, bool>> joinExpression,
            Expression<Func<T, TJoin, TResult>> selectExpression,
            Expression<Func<T, TJoin, bool>> whereExpression,
            int pageSize,
            int page);

        void Update<T>(Expression<Func<T, T>> setExpression, Expression<Func<T, bool>> whereExpression, DbTransaction transaction = null);

        void Update<T>(Expression<Func<T, T>> setExpression, string id, string keyName = "Id", DbTransaction transaction = null);

        void BatchInsert<T>(IEnumerable<T> entities, DbTransaction transaction = null);

        void BatchUpdate<T>(IEnumerable<T> entities, Expression<Func<T, T>> setExpression, Expression<Func<T, bool>> whereExpression, DbTransaction transaction = null);


        void BatchUpdate<T>(IEnumerable<T> entities, string keyName = "Id", DbTransaction transaction = null);



        Base.TableInfo GetTableInfo(string tableName);


        List<string> GetTbNameList();

    }
}
